<script>
import { Button, Dialog, Field, Form, Picker, Popup, Toast } from 'vant';
import copy from 'copy-to-clipboard';
import { api } from '@/api';
import { mapStores } from 'pinia';
import { useAppStore } from '@/stores/app';

export default {
  components: {
    Field,
    Picker,
    Popup,
    Form,
    Button
},
  computed: {
    ...mapStores(useAppStore)
  },
  data() {
    return {
      coinType: { text: 'USDT-TRC20', value: 'USDT-TRC20' },
      columns: [
        { text: 'USDT-TRC20', value: 'USDT-TRC20' }
      ],
      address: '',
      amount: 2,
      info: {},
      showPicker: false,
    }
  },
  async created() {
    if (this.appStore.balance < 2) {
      this.amount = this.appStore.balance
    }
    const loading = Toast.loading({ message: '加载中...', overlay: true })
    const { data } = await api.coinAccountInfo()
    loading.clear()
    this.info = data
  },
  methods: {
    onConfirm(selected) {
      this.coinType = {...selected}
      this.showPicker = false
    },
    copyAddress() {
      if (copy(this.info?.charge_addr)) {
        Toast.success({ message: '复制成功' })
      }
    },
    fillFullCoin() {
      this.amount = this.appStore.balance
    },
    async onSubmit(data) {
      const confirm = await Dialog.confirm({
        title: '确认提现',
        message: `确认提现${data.amount}USDT到${data.address}吗?`,
        confirmButtonText: '确认',
        cancelButtonText: '取消',
      }).catch(() => {})

      if (confirm !== 'confirm') return

      const { success, msg } = await api.wallet.withapply({ price: data.amount, addr: data.address })

      if (success) {
        Toast.success({ message: '提现申请已提交' })
        this.appStore.refreshBalance()
      } else {
        Toast.fail({ message: msg ?? '提现申请提交失败' })
      }
    }
  },
}
</script>

<template>
  <div class="withdraw-coin">
    <Form @submit="onSubmit" style="width: 100%;">
      <Field
        :value="coinType.text"
        is-link
        readonly
        label="提币网络"
        placeholder="选择提币网络"
        @click="showPicker = true"
        name="coinType"
      />
      <Field
        v-model="address"
        label="提币地址"
        placeholder="请输入提币地址"
        name="address"
        :rules="[{ required: true, message: '请填写提币地址' }]"
      />
      <Field
        v-model.number="amount"
        label="提币数量"
        type="number"
        placeholder="请输入提币数量"
        name="amount"
        :rules="[
          { required: true, message: '请填写提币数量' },
          { validator: (val) => val >= 2, message: '最小提币数量为2USDT' },
          { validator: (val) => val < appStore.balance, message: '余额不足' }
        ]"
      >
        <template #extra>
          <div class="withdraw-coin-extra">
            <div>USDT</div>
            <div class="withdraw-coin-extra-link" @click="fillFullCoin">全部</div>
          </div>
        </template>
      </Field>
      <div class="withdraw-coin-available">
        <div>可提取(不含手续费)</div>
        <div>{{ appStore.balance }} USDT</div>
      </div>
      <Field
        :value="info?.handle_charge"
        readonly
        label="手续费"
        placeholder="手续费"
        name="charge"
      >
        <template #extra>USDT</template>
      </Field>

      <div class="withdraw-coin-submit">
        <Button type="primary" round block native-type="submit" :disabled="appStore.balance < 2">
          提交
        </Button>
      </div>
    </Form>
    
    <Popup v-model="showPicker" round position="bottom">
      <Picker
        show-toolbar
        :columns="columns"
        @cancel="showPicker = false"
        @confirm="onConfirm"
      />
    </Popup>

    <div class="add-coin-tips">
      <div class="add-coin-tips-title">注意：</div>
      <div class="add-coin-tips-text">最小提币金额：2USDT</div>
      <div class="add-coin-tips-text">手续费为单笔1USDT</div>
    </div>
  </div>
</template>

<style lang="less" scoped>
.withdraw-coin {
  display: flex;
  flex-direction: column;
  align-items: start;

  height: 100%;

  .withdraw-coin-available {
    display: flex;
    gap: 20px;
    justify-content: space-between;
    font-size: 14px;
    width: 100%;
    padding: 15px;
    box-sizing: border-box;

    div:first-child {
      color: #646566;
    }
  }

  .withdraw-coin-extra {
    display: flex;
    gap: 10px;

    .withdraw-coin-extra-link {
      color: #1989fa;
    }
  }

  .add-coin-tips {
    display: flex;
    flex-direction: column;
    justify-content: flex-end;
    // flex: 1;
    padding: 20px;
    // 浅红色
    color: #f56c6c;
    .add-coin-tips-title {
      font-size: 14px;
      font-weight: bold;
    }
    .add-coin-tips-text {
      font-size: 13px;
      margin-top: 10px;
    }
  }

  .withdraw-coin-submit {
    width: 100%;
    padding: 20px 10px;
    box-sizing: border-box;
  }
}
</style>
